﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AutoUpdateMgr.Mgr
{
    internal class UploadMgr
    {
        private static UploadMgr instance { set; get; } = null;

        internal static UploadMgr getInstance()
        {
            if (instance == null)
            {
                instance = new UploadMgr();
            }
            return instance;
        }


        private FormFileUpload frmFileUpload { set; get; } = null;

        private AutoUpdate.Model.Model.fileTransfer.fs_version currentVersion { set; get; } = null;

        private List<AutoUpdate.Model.Model.fileTransfer.fs_inode> files { set; get; } = null;

        private AutoUpdate.Model.Model.fileTransfer.fs_inode currentNode { set; get; } = null;

        private long fileOffset { set; get; } = 0;

        internal void upload(AutoUpdate.Model.Model.fileTransfer.fs_version currentVersion, FormFileUpload frmFileUpload, List<AutoUpdate.Model.Model.fileTransfer.fs_inode> files)
        {
            if (currentVersion != null && frmFileUpload != null && files != null)
            {
                this.currentVersion = currentVersion;

                this.frmFileUpload = frmFileUpload;

                this.files = files;
            }
            else
            {
                return;
            }

            new Task(() =>
            {
                try
                {
                    for (int i = 0; i < files.Count; i++)
                    {
                        currentNode = files[i];

                        if (currentNode != null)
                        {
                            this.frmFileUpload.changeCurrentFileName("正在上传=>" + this.currentNode.name);

                            //this.frmFileUpload.setFileUploadCount(files.Count);

                            this.frmFileUpload.setFileUploadIndex(i);

                            this.fileOffset = 0;

                            this.uploadFile();
                        }
                    }
                    this.frmFileUpload.changeCurrentFileName("上传完毕");

                    AutoUpdate.Model.Packet.RequestPacket request1 = new AutoUpdate.Model.Packet.RequestPacket();

                    request1.CommandText = "updateVersion";

                    request1.MessageContent = AutoUpdate.Utility.Helper.JsonHelper.getString(this.currentVersion);

                    ClientMgr.getInstance().client.sendMessage(AutoUpdate.Utility.Helper.JsonHelper.getString(request1));
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }

            }).Start();


        }

        private void uploadFile()
        {
            if (this.currentNode == null)

                return;

            int fileBlockLength = 32 * 1024;

            int length = 0;

            byte[] fileBuffer = null;

            try
            {
                fileBuffer = AutoUpdate.Utility.Helper.FileHelper.fileRead(this.currentNode.realPath, this.fileOffset, fileBlockLength, out length);
            }
            catch (Exception ex)
            {
                return;
            }



            AutoUpdate.Model.Model.fileTransfer.fs_inodeContent nodeModel = new AutoUpdate.Model.Model.fileTransfer.fs_inodeContent();

            nodeModel.length = length;

            nodeModel.md5 = this.currentNode.md5;

            nodeModel.offset = this.fileOffset;

            nodeModel.version = this.currentVersion.version;

            nodeModel.fileLength = this.currentNode.length;

            nodeModel.contents = fileBuffer;

            AutoUpdate.Model.Packet.RequestPacket request = new AutoUpdate.Model.Packet.RequestPacket();

            request.CommandText = "uploadFile";

            request.MessageContent = AutoUpdate.Utility.Helper.JsonHelper.getString(nodeModel);

            ActionMgr.getInstance().addAction(request.PacketID, new Action<AutoUpdate.Model.Packet.Packet>((AutoUpdate.Model.Packet.Packet packet) =>
            {
                Console.WriteLine("Thread.Sleep(50000);");

                AutoUpdate.Model.Model.fileTransfer.fs_inodeContent inodeModel = AutoUpdate.Utility.Helper.JsonHelper.getObject<AutoUpdate.Model.Model.fileTransfer.fs_inodeContent>(packet.MessageContent);

                if (inodeModel != null)
                {
                    this.fileOffset += inodeModel.length;

                    this.frmFileUpload.setFileUploadLength((int)this.currentNode.length);

                    this.frmFileUpload.setFileUploadOffset((int)this.fileOffset);

                    if (fileOffset == inodeModel.fileLength)
                    {
                        this.fileOffset = 0;
                    }
                    else
                    {
                        this.uploadFile();
                    }
                     
                }

            }));

            ClientMgr.getInstance().client.sendMessage(AutoUpdate.Utility.Helper.JsonHelper.getString(request));
        }
    }
}
